package com.xxg.renrenproduct.controller;

import com.xxg.renrencommon.domain.constant.AccountConstant;
import com.xxg.renrencommon.domain.result.BaseResult;
import com.xxg.renrencommon.domain.result.PageInfoResult;
import com.xxg.renrenproduct.dao.bean.UploadGoodsInfo;
import com.xxg.renrenproduct.domain.query.*;
import com.xxg.renrenproduct.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 商品管理
 *
 * @author szj
 * @date 2021/12/28
 */
@Api(tags = "商品管理")
@RestController
@RequestMapping("/product")
@AllArgsConstructor
@Slf4j
public class ProductController {
    private final ProductService productService;

    @ApiOperation("上传商品")
    @RequestMapping(value = "upload", method = RequestMethod.POST)
    public BaseResult<Map<String, String>> upload(@RequestPart("文件") MultipartFile file) throws IOException {
        String contentType = file.getContentType();
        log.info("contentType:{}", contentType);
        String originalFilename = file.getOriginalFilename();
        if (originalFilename == null || !originalFilename.endsWith(".json")) {
            return new BaseResult<>("文件格式错误，需要JSON文件", null);
        }
        return productService.upload(file);
    }

    @ApiOperation("删除商品")
    @RequestMapping(value = "delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "删除商品参数", required = true, dataType = "DeleteProductQuery")})
    public BaseResult<Void> delete(@RequestBody @Validated DeleteProductQuery query){
        return productService.delete(query);
    }

    @ApiOperation("查询商品上传信息")
    @RequestMapping(value = "getUploadInfo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "查询商品上传信息参数", required = true, dataType = "GetUploadInfoQuery")})
    public BaseResult<PageInfoResult<UploadGoodsInfo>> getUploadInfo(@RequestBody @Validated GetUploadInfoQuery query){
        return productService.getUploadInfo(query);
    }
}
